if (appBosch.line != null) {
    var urlRead = appBosch.crud.read + '/' + appBosch.line;
} else {
    var urlRead = appBosch.crud.read;
}

$(document).ready(function () {
    dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: urlRead,
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'read':
                        break;
                }
                return params;
            }
        }
    });

    grid = $("#grid").kendoGrid({
        toolbar: [
            {
                name: "pdf",
                text: "Exportar a PDF"
            },
            {
                name: "excel",
                text: "Exportar a Excel"
            }
        ],
        excel: {
            fileName: "Mis certificaciones en entrenamiento.xlsx",
            filterable: true,
            allPages: true
        },
        pdf: {
            allPages: true,
            fileName: "Mis certificaciones en entrenamiento.pdf"
        },
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        resizable: true,
        columnMenu: true,
        sortable: true,
        dataSource: dataSource,
        dataBound: function(e) {
            appBosch.iTrainings = 1;
        },
        groupable: true,
        schema: {
            model: {
                id: "value",
                fields: {
                    start: {type: 'date'},
                    end: {type: 'date'}
                }
            }
        },
        columns: [
            {
                title: "#",
                template: '#=appBosch.iTrainings++#',
                width:50
            },
            {
                field: "line", title: "Línea",
                width: 100
            },
            {
                field: "workstation", 
                title: "Estación"
            },
            {
                field: "start",
                title: "Inicio del proceso",
                template: '<span title="#=kendo.toString(new Date(start.replace(/-/g, "/")), "D")#" >#=kendo.toString(new Date(start.replace(/-/g, "/")), "d")#</span>'
            },
            {
                field: "end",
                title: "Fin del proceso",
                template: '<span title="#=kendo.toString(new Date(end.replace(/-/g, "/")), "D")#" >#=kendo.toString(new Date(end.replace(/-/g, "/")), "d")#</span>'
            },
            {
                field: "proximos", 
                title:  " ",
                template: kendo.template($("#templateProximos").html()),
                width:50
            },
            {
                field: "ws_id", 
                title: " ",
                template: '<input type="hidden" name="workstation_id" value="#=ws_id#">',
                hidden: true
            },
            {
                field: 'dependences',
                title: "Opciones",
                template: '# if (dependences <= 0) { # <a class="k-button k-button-icontext startExam" href="\\#">Iniciar certificación en piso</a> # } #'
            }
        ]
    }).data('kendoGrid');

    $('#grid').on('click', '.startExam', startExam);

    $('#grid table').on('dblclick', 'tr', showDetails2);
});

function startExam(e)
{
    e.preventDefault();
    var url = appBosch.urlStartExam + '/index/' + $(e.currentTarget).closest("tr").find('td input[type=hidden]').val();
    $(location).attr('href', url);
}

function showDetails2(e) {
    var selectedRows = grid.select();
    var dataItem = grid.dataItem(selectedRows[0]);
    var id = dataItem.value;
    var contentTemp = $("#templateDetalles").html();
    var temp = kendo.template(contentTemp);

    $.get(appBosch.urlDetails + '/' + id, {}, function (data) {
        var window = $("<div></div>");
        var info = { data: data };
        window.html(temp(info));
        window.kendoWindow({
            modal: true,
            minWidth: "1000px",
            title: dataItem.line + ' - ' + dataItem.workstation,
            actions: [
                "Close"
            ]
        });
        window.bind("deactivate", function () {
            window.destroy();
        });
        window.data("kendoWindow").open().center();
    });
}